<?php

namespace app\admin\controller;


use app\common\lib\Status;

class Category extends AdminBase
{
    public function index()
    {
        //获取pid信息 顶级分类默认为0
        $pid = input("param.pid", 0, "intval");
        $data = [
            "pid" => $pid,
        ];

        //获取分页逻辑
        try {
            $categorys = (new \app\common\business\Category())->getLists($data, 5);
        } catch (\Exception $e) {
            $categorys = [];
        }
        //  halt($categorys);


        return view('', [
            "categorys" => $categorys,
            "pid" => $pid,
        ]);
    }

    //添加
    public function add()
    {
        try {
            $categorys = (new \app\common\business\Category())->getNormalCategorys();
        } catch (\Exception $e) {
            $categorys = [];
        }
        // halt($categorys);
        return view("", ['categorys' => json_encode($categorys)]);
    }


    /**
     * 编辑页面
     * @return \think\response\View
     */
    public function edit()
    {

        try {
            $categorys = (new \app\common\business\Category())->getNormalCategorys();
        } catch (\Exception $e) {
            $categorys = [];
        }
        //获取数据
        $id = input("param.id", 0, "intval");
        try {
            $user = (new \app\common\business\Category())->getById($id);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        // halt($categorys);
        return view("", ['categorys' => json_encode($categorys), 'name' => $user]);


    }

    /**
     * 编辑逻辑
     * @return \think\response\Json
     */
    public function update()
    {
        //   获取参数
        $id = input("param.id", 0, "intval");
        $name = input("param.name", "", "trim");

        //校验
        $data = [
            'id' => $id,
            'name' => $name,
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('update')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //编辑逻辑
        try {
            $result = (new \app\common\business\Category())->update($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return show(config('status.success'), "ok");
        }
        return show(config('status.error'), "编辑失败");
    }


    /**
     * 新增逻辑
     * @return \think\response\Json
     */
    public function save()
    {
        //获取参数
        $pid = input("param.pid", 0, "intval");
        $name = input("param.name", "", "trim");

        //校验
        $data = [
            'pid' => $pid,
            'name' => $name,
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('save')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //新增逻辑
        try {
            $result = (new \app\common\business\Category())->add($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($result) {
            return show(config('status.success'), "ok");
        }
        return show(config('status.error'), "新增失败");
    }

    /**
     * 排序
     * @return \think\response\Json
     */
    public function listorder()
    {
        //获取数据
        $id = input("param.id", 0, "intval");
        $listorder = input("param.listorder", 0, "intval");

        //校验
        $data = [
            'id' => $id,
            'listorder' => $listorder
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('listorder')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        //排序逻辑
        try {
            $res = (new \app\common\business\Category())->listorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return show(config('status.success'), "排序成功");
        } else {
            return show(config('status.error'), '排序失败');
        }


    }

    /**
     * 更新状态
     * @return \think\response\Json
     */
    public function status()
    {

        //接收数据
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");

        //验证
        $data = [
            'status' => $status,
            'id' => $id,
        ];
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('status')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        //如果id不存在  或者 状态码里 没这四个就是错误参数
        if (!$id || !in_array($status, Status::getTableStatus())) {
            return show(config('status.error'), '参数错误');
        }

        //更新状态逻辑
        try {
            $res = (new \app\common\business\Category())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return show(config('status.success'), '更新状态成功');
        } else {
            return show(config('status.error', '更新状态是不'));
        }


    }

    //展示一级分类
    public function dialog()
    {
        //获取正常的一级分类数据
        $categorys = (new \app\common\business\Category())->getNormalByPid();
        return view("", ["categorys" => json_encode($categorys)]);
    }


    //获取二级分类
    public function getByPid()
    {
        $pid = input("param.pid", 0, "intval");
        $categorys = (new \app\common\business\Category())->getNormalByPid($pid);

        return show(config("status.success"), "ok", $categorys);

    }
}
